package com.zhiyou100.util.mysql;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author yang
 * @className C3p0Util
 * @packageName com.zhiyou100.C3p0Util
 * @Description TODO jdbc 工具类
 * @date 2020-08-06 15:35
 */
public class C3p0Util {
    public static DataSource DATA_SOURCE;

    static {
        DATA_SOURCE = new ComboPooledDataSource();
        //  配置文件放在 resources 内部
    }


    public static Connection getCon() {
        try {
            return DATA_SOURCE.getConnection();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
